package com.jy.appmarket.cards;

import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.jy.app.market.apk.type.LinkType;
import com.jy.app.market.idata.card.CardGroupTop;
import com.jy.app.market.idata.data.Link;
import com.jy.app.market.idata.data.Top;
import com.jy.appmarket.R;
import com.jy.appmarket.activity.AppDetailActivity;
import com.jy.appmarket.activity.CardPageActivity;
import com.jy.appmarket.activity.PhoneEssentialActivity;
import com.jy.appmarket.activity.WebViewActivity;
import com.jy.appmarket.statistics.ApkSource;
import com.jy.appmarket.utils.LoadImageUtils;
import com.nostra13.universalimageloader.core.ImageLoader;

/**
 * 类说明：
 * 
 * @author Baker.li
 * @date 2015年8月21日
 * @version 1.0
 */

public class GroupTopUICard extends BaseUICard {

	private CardGroupTop mGroupTop;
	private int mGroupCount;

	public GroupTopUICard(Context context, CardGroupTop groupTop) {
		super(context, R.layout.group_top_card_layout);
		mGroupTop = groupTop;
		init();
	}

	private void init() {
		if (mGroupTop == null) {
			return;
		}
		List<Top> tops = mGroupTop.getTops();
		if (tops == null || tops.isEmpty()) {
			mGroupCount = 0;
		} else {
			mGroupCount = (tops.size() >= 4 ? 4 : tops.size());
		}
	}

	@Override
	public int getType() {
		return 11;
	}

	@Override
	public void setupInnerViewElements(ViewGroup parent, View view) {
		super.setupInnerViewElements(parent, view);
		View views[] = new View[] { parent.findViewById(R.id.group_top_group1),
				parent.findViewById(R.id.group_top_group2),
				parent.findViewById(R.id.group_top_group3),
				parent.findViewById(R.id.group_top_group4) };
		for (View v : views) {
			v.setVisibility(View.GONE);
		}

		if (mGroupCount <= 0) {
			return;
		}
		for (int i = 0; i < mGroupCount; i++) {
			Top top = mGroupTop.getTops().get(i);
			views[i].setVisibility(View.VISIBLE);
			ImageView icon = views[i].findViewById(R.id.group_item_image);
			TextView name = views[i].findViewById(R.id.group_item_text);
			
			//ImageLoader.getInstance().displayImage(top.getImageUrl(), icon);
			
			ImageLoader.getInstance().displayImage(top.getImageUrl(), icon,
					LoadImageUtils.getGroupTopIconDisplayOption(getContext()));
			
			name.setText(top.getTitle());
			final Link link = top.getLink();
			if (link == null) {
				views[i].setOnClickListener(null);
				return;
			}

			views[i].setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {

					if (LinkType.App == link.getType()) {
						Intent detail = new Intent(getContext(), AppDetailActivity.class);
						detail.putExtra(AppDetailActivity.EXTRA_NAME, link.getTitle());
						detail.putExtra(AppDetailActivity.EXTRA_PKG, link.getUrl());
						detail.putExtra(AppDetailActivity.EXTRA_SOURCE,
								ApkSource.toSourceString(getApkSource()));
						getContext().startActivity(detail);

					} else if (LinkType.Cards == link.getType()) {
						if ("essential".equals(link.getUrl())) {
							getContext().startActivity(
									new Intent(getContext(), PhoneEssentialActivity.class));
						} else {
							Intent detail = new Intent(getContext(), CardPageActivity.class);
							detail.putExtra(CardPageActivity.EXTRA_TITLE, link.getTitle());
							detail.putExtra(CardPageActivity.EXTRA_PATH, link.getUrl());
							getContext().startActivity(detail);
						}
					} else if (LinkType.WebView == link.getType()) {
						Intent detail = new Intent(getContext(), WebViewActivity.class);
						detail.putExtra(WebViewActivity.EXTRA_TITLE, link.getTitle());
						detail.putExtra(WebViewActivity.EXTRA_URL, link.getUrl());
						getContext().startActivity(detail);

					}
				}
			});

		}

	}
}